package com.ty.xiaoyu.service.impl;

import com.ty.xiaoyu.entity.Topic;
import com.ty.xiaoyu.entity.TopicImage;
import com.ty.xiaoyu.exception.TopicOperationException;
import com.ty.xiaoyu.service.TopicService;
import com.ty.xiaoyu.util.MyTimeUtil;
import com.ty.xiaoyu.util.PageCalculatorUtil;
import com.ty.xiaoyu.util.RandomIdUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class TopicServiceImpl extends BaseService implements TopicService {
    @Override
    public List<Topic> getListByReleaseTime(Integer pageIndex, Integer pageSize) throws TopicOperationException {
        Integer rowIndex = PageCalculatorUtil.calculateRowIndex(pageIndex, pageSize);
        List<Topic> topicList = topicMapper.queryListByReleaseTime(rowIndex, pageSize);
        if (topicList == null || topicList.isEmpty()) {
            throw new TopicOperationException("根据发布时间未找到帖子");
        }
        return topicList;
    }

    @Override
    public List<Topic> getListByLastTime(Integer pageIndex, Integer pageSize) throws TopicOperationException {
        Integer rowIndex = PageCalculatorUtil.calculateRowIndex(pageIndex, pageSize);
        List<Topic> topicList = topicMapper.queryListByLastTime(rowIndex, pageSize);
        if (CollectionUtils.isEmpty(topicList)) {
            throw new TopicOperationException("根据最后回复时间未找到帖子");
        }
        return topicList;
    }

    @Override
    public Integer queryTopicNum() {
        return topicMapper.queryListCount();
    }

    @Override
    public List<TopicImage> getTopicImageList(String topicId) {
        List<TopicImage> topicImageList = topicImageMapper.queryTopicImageList(topicId);
        return ListUtils.defaultIfNull(topicImageList, new LinkedList<>());
    }

    @Override
    public List<Topic> getListByCondition(Integer pageIndex, Integer pageSize, List<String> condition) throws TopicOperationException {
        Integer rowIndex = PageCalculatorUtil.calculateRowIndex(pageIndex, pageSize);
        List<Topic> topicList = topicMapper.queryListByCondition(rowIndex, pageSize, condition);
        if (CollectionUtils.isEmpty(topicList)) {
            throw new TopicOperationException("为搜索到帖子");
        }
        return topicList;
    }

    @Override
    public Topic addTopic(Topic topic, List<String> topicImagePathList) throws TopicOperationException {
        topic.setTopicId(RandomIdUtil.getId());
        topic.setTopicTime(MyTimeUtil.nowTime());
        topic.setTopicState(1);
        topic.setLastReplyTime(MyTimeUtil.nowTime());
        topic.setGreatNum(0);
        topic.setBrowseNum(0);
        Integer topicState = topicMapper.insertTopic(topic);
        if (topicState <= 0) {
            throw new TopicOperationException("帖子添加失败");
        }
        List<TopicImage> topicImageList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(topicImagePathList)) {
            for (int i = 0; i < topicImagePathList.size(); i++) {
                String topicImagePath = topicImagePathList.get(i);
                TopicImage topicImage = new TopicImage();
                topicImage.setTopicImageId(RandomIdUtil.getId());
                topicImage.setTopicImagePath(topicImagePath);
                topicImage.setTopicImageState(1);
                topicImage.setCreateTime(MyTimeUtil.nowTime());
                topicImage.setTopicId(topic.getTopicId());
                topicImage.setTopicImagePriority(10 - i);
                topicImageList.add(topicImage);
            }
            Integer topicImageState = topicImageMapper.insertTopicImage(topicImageList);
            if (topicImageState <= 0) {
                throw new TopicOperationException("帖子图片添加失败");
            }
        }
        return topic;
    }

    @Override
    public Topic getTopicById(String topicId) throws TopicOperationException {
        Topic topic = topicMapper.queryTopicById(topicId);
        if (topic == null) {
            throw new TopicOperationException("未找到帖子");
        }
        return topic;
    }

    @Override
    public List<Topic> getListByModuleId(Integer pageIndex, Integer pageSize, String moduleId) {
        Integer rowIndex = PageCalculatorUtil.calculateRowIndex(pageIndex, pageSize);
        List<Topic> topicList = topicMapper.queryListByModuleId(rowIndex, pageSize, moduleId);
        return ListUtils.defaultIfNull(topicList, new LinkedList<>());
    }

    @Override
    public Topic getTopicByIdForUpdate(String topicId) throws TopicOperationException {
        Topic topic = topicMapper.queryTopicByIdForUpdate(topicId);
        if (topic == null) {
            throw new TopicOperationException("未找到帖子");
        }
        return topic;
    }

    @Override
    public void editGreatNum(Topic topic) throws TopicOperationException {
        Integer state = topicMapper.updateGreatNum(topic);
        if (state <= 0) {
            throw new TopicOperationException("点赞数修改失败");
        }
    }

    @Override
    public void editBrowseNum(Topic topic) throws TopicOperationException {
        Integer state = topicMapper.updateBrowseNum(topic);
        if (state <= 0) {
            throw new TopicOperationException("浏览数修改失败");
        }
    }

    @Override
    public List<Topic> getTopicListByUserId(String userId) throws TopicOperationException {
        List<Topic> topicList = topicMapper.queryTopicListByUserId(userId);
        if (CollectionUtils.isEmpty(topicList)) {
            throw new TopicOperationException("未找到我的帖子");
        }
        return topicList;
    }

}
